/// **************************************************************************************************
/// "Copyright (c) 2007 Sai Panyam, Inc.  All rights reserved."
/// **************************************************************************************************
/// Component Name   :	Operation, Member of System72.NTextile 
/// Description      :	Operation description
/// Author           :	Sai Panyam
/// Creation Date    :	November 09 2007
/// Modified Date    :              
/// Modified By      :
/// **************************************************************************************************
using System;
using System.Text.RegularExpressions;

namespace System72.NTextile
{
    /// <summary>
    /// 
    /// </summary>
    internal class Operation
    {
        #region Private Variables

        private string m_tag;
        private string m_operator;
        private string m_regExOperator;
        private ModifierType m_modifierType;

        #endregion

        #region Properties

        public string Tag
        {
            get
            {
                return m_tag;
            }
            set
            {
                m_tag = value;
            }
        }

        public string Operator
        {
            get
            {
                return m_operator;
            }
            set
            {
                m_operator = value;
            }
        }


        public string RegExOperator
        {
            get
            {
                return m_regExOperator;
            }
            set
            {
                m_regExOperator = value;
            }
        }

        public ModifierType ModifierType
        {
            get { return m_modifierType; }
            set { m_modifierType = value; }
        }

        #endregion

        #region Public Methods

        public string GetAttributeText(string match, Attribute modAttribute, out string matchValue)
        {
            //TODO: Change to Regex library later
            string open = (modAttribute.OperatorOpen == "[") ? @"\[" : modAttribute.OperatorOpen;
            string close = (modAttribute.OperatorClose == "]") ? @"\]" : modAttribute.OperatorClose;
            string regexOpOpen = @"\" + ((modAttribute.OperatorOpen == "[") ? modAttribute.OperatorOpen : open);
            string regexOpClose = @"\" + ((modAttribute.OperatorClose == "]") ? modAttribute.OperatorClose : close);
            string pattern;
            if (open != "(#")
            {
                
                if (modAttribute.AttributeType == AttributeType.ClassID)
                {
                    pattern = String.Format(@"{0}(?>[^#{1}{2}]+#[^#{1}{2}]+|{0}(?<number>)|{3}(?<-number>))*(?(number)(?!)){3}", regexOpOpen, open, close, regexOpClose);
                }
                else//Exclude the # if present for all attributes except classID
                {
                    pattern = String.Format(@"{0}(?>[^#{1}{2}]+|{0}(?<number>)|{3}(?<-number>))*(?(number)(?!)){3}", regexOpOpen, open, close, regexOpClose);
                }
            }
            else
            {
                regexOpOpen = @"\(\#";
                //Include the # in the pattern search
                pattern = String.Format(@"{0}(?>[^{1}{2}]+|{0}(?<number>)|{3}(?<-number>))*(?(number)(?!)){3}", regexOpOpen, open, close, regexOpClose);
            }
            Regex regEx = new Regex(pattern);
            Match matchedText = regEx.Match(match);
            matchValue = matchedText.Value;
            //Remove the open and close Operator symbol from the matched Text. By starting and ending between the operators
            return (matchValue.Length > 2) ? matchValue.Substring(modAttribute.OperatorOpen.Length, matchValue.Length - (modAttribute.OperatorOpen.Length + modAttribute.OperatorClose.Length)) : String.Empty;
        }

        #endregion
    }
}